#include "LogicUndo.h"

LogicUndo& LogicUndo::getInstance() {
	if( _logicUndo == NULL ) {
		_logicUndo = new LogicUndo;
	}

	return *_logicUndo;
}

string LogicUndo::undo(){

        string statusMessage;
		vector<Task> tasksVector;
        
        try{
                
                if( undoStackData.size() != 1 ) {
                
                tasksVector.clear();
                undoStackData.pop();
                undoStackData.top().getAll( tasksVector );
                // load and write will destroy the contents if tasksvector is subdisplay of the orginal 
                // list of tasks
                _writer->write( tasksVector );
                _data->executeLoad( tasksVector );
                statusMessage = UNDO_SUCCESS_MESSAGE;
        }
        else {
                statusMessage = UNDO_ERROR_MESSAGE;
        }
        return statusMessage;
   }
        catch( CustomException& e ){
                statusMessage = "Exception : "+e.getMessage();
                return statusMessage;
        }
        catch (...) {
                statusMessage = "Unknown exception ";
                return statusMessage;
        }
}

const string LogicUndo::UNDO_SUCCESS_MESSAGE = "Undo Successful";

const string LogicUndo::UNDO_ERROR_MESSAGE = "Unable to Undo Further";

LogicUndo* LogicUndo::_logicUndo = NULL;